##########################################################################################################################################################
### Setup

### Source setup
source("code/1_modeling_setup.R")



##########################################################################################################################################################
### Models of interchamber IVs and pr(vote)

### Models

# Run divided government interaction models
mods_vote_divided_inter_int <- vector(mode='list', length=2)
mods_vote_divided_inter_int[[1]] <- glm(voted_sles ~ veto_override_threshold*divided3 + lineitemveto + maj_sponsor + num_cs + maj_share + pty_diff + mds1 + committee_sponsor + maj_unity + gov_pty_override + as.factor(year), family='binomial', data=filter(bill, !state %in% high_missingness))
mods_vote_divided_inter_int[[2]] <- glm(voted_sles ~ lineitemveto_plot*divided3 + veto_override_threshold + maj_sponsor + num_cs + maj_share + pty_diff + mds1 + committee_sponsor + maj_unity + gov_pty_override + as.factor(year), family='binomial', data=filter(bill, !state %in% high_missingness))


### Substantive effect sizes

# DV observed proportion
vote_sd <- bill %>% 
  filter(!state %in% high_missingness) %>%
  pull(voted_sles) %>%
  sd()
 
# Veto override threshold
eff_veto_vote <- plot_model(mods_vote_divided_inter_int[[1]], type='pred', 
                            terms=c('veto_override_threshold', 'divided3'))$data %>% 
  as.data.frame() %>%
  filter(x!=0.6) %>%
  dplyr::select(x, predicted, group) %>%
  group_by(group) %>%
  summarise(
    eff_size = diff(predicted),
    pred_x = predicted[x==0.5],
    x_text = min(x)
  ) %>%
  mutate(eff_size_dv=abs(eff_size/vote_sd))

# Line-item veto
eff_line_vote <- plot_model(mods_vote_divided_inter_int[[2]], type='pred', terms=c('lineitemveto_plot', 'divided3'))$data %>% 
  as.data.frame() %>%
  dplyr::select(x, predicted, group) %>%
  group_by(group) %>%
  summarise(
    eff_size = diff(predicted),
    pred_x = predicted[x==2],
    x_text = max(x)
  ) %>%
  mutate(eff_size_dv = abs(eff_size/vote_sd))


### Predicted effects

# Veto override threshold
plot_vote_divided_veto <- plot_model(mods_vote_divided_inter_int[[1]], type='pred', line.size=2, dot.size=5,
                                     terms=c('veto_override_threshold', 'divided3'), colors='bw') +
  labs(x='Veto Override Threshold', y='Probability of Reaching a Vote', title='', linetype='') + 
  theme_bw() +
  geom_text(data=eff_veto_vote, aes(x=x_text, y=pred_x, label=paste0(round(eff_size_dv, 2), ' SDs'), group=eff_size_dv), 
            inherit.aes = FALSE, hjust=-0.1, vjust=-0.2, size=6) +
  theme(text=element_text(size=16),
        axis.text=element_text(size=16),
        legend.position="none")
  
# Line item veto
plot_vote_divided_line <- plot_model(mods_vote_divided_inter_int[[2]], type='pred', line.size=2, dot.size=5,
                                     terms=c('lineitemveto_plot', 'divided3'), colors='bw') +
  geom_line(linetype='dotted', linewidth=1.2, position=position_dodge(width=0.1)) +
  labs(x='Line-Item Veto', y='Probability of Reaching a Vote', title='', shape='') + 
  theme_bw() +
  geom_text(data=eff_line_vote, aes(x=x_text, y=pred_x, label=paste0(round(eff_size_dv, 2), ' SDs'), group=eff_size_dv), 
            inherit.aes = FALSE, hjust=1.2, position=position_dodge(width=0.1), size=6)  +
  theme(text=element_text(size=16),
        axis.text=element_text(size=16),
        legend.position="none") 



##########################################################################################################################################################
### Models of interchamber IVs and pr(law)

### Models

# Run divided government interaction models
mods_law_divided_inter_int <- vector(mode='list', length=2)
mods_law_divided_inter_int[[1]] <- glm(law_sles ~ veto_override_threshold*divided3 + lineitemveto + maj_sponsor + num_cs + maj_share + pty_diff + mds1 + committee_sponsor + maj_unity + gov_pty_override + as.factor(year), family='binomial', data=filter(bill, !state %in% high_missingness))
mods_law_divided_inter_int[[2]] <- glm(law_sles ~ lineitemveto_plot*divided3 + veto_override_threshold + maj_sponsor + num_cs + maj_share + pty_diff + mds1 + committee_sponsor + maj_unity + gov_pty_override + as.factor(year), family='binomial', data=filter(bill, !state %in% high_missingness))


### Substantive effect sizes

# DV observed proportion
law_sd <- bill %>%
  filter(!state %in% high_missingness) %>%
  pull(law_sles) %>%
  sd()

# Veto override threshold
eff_veto_law <- plot_model(mods_law_divided_inter_int[[1]], type='pred', terms=c('veto_override_threshold', 'divided3'))$data %>% 
  as.data.frame() %>%
  filter(x!=0.6) %>%
  dplyr::select(x, predicted, group) %>%
  group_by(group) %>%
  summarise(
    eff_size = diff(predicted),
    pred_text = predicted[x==0.50],
    x_text = min(x)
  ) %>%
  mutate(eff_size_dv=abs(eff_size/law_sd))

# Line-item veto
eff_line_law <- plot_model(mods_law_divided_inter_int[[2]], type='pred', terms=c('lineitemveto_plot', 'divided3'))$data %>% 
  as.data.frame() %>%
  dplyr::select(x, predicted, group) %>%
  group_by(group) %>%
  summarise(
    eff_size = diff(predicted),
    pred_text = predicted[x==2],
    x_text = max(x)
  ) %>%
  mutate(eff_size_dv = abs(eff_size/law_sd))


### Predicted effects

# Veto override threshold
plot_law_divided_veto <- plot_model(mods_law_divided_inter_int[[1]], type='pred', line.size=2, dot.size=5,
                                    terms=c('veto_override_threshold', 'divided3'), colors='bw') +
  labs(x='Veto Override Threshold', y='Probability of Becoming Law', title='', linetype='') + 
  theme_bw() +
  geom_text(data=eff_veto_law, aes(x=x_text, y=pred_text, label=paste0(round(eff_size_dv, 2), ' SDs'), group=eff_size_dv), 
            inherit.aes = FALSE, hjust=-0.01, vjust=-0.5, size=6)  +
  theme(text=element_text(size=16),
        axis.text=element_text(size=16),
        legend.position="none")

# Line item veto
plot_law_divided_line <- plot_model(mods_law_divided_inter_int[[2]], type='pred', line.size=2, dot.size=5,
                                    terms=c('lineitemveto_plot', 'divided3'), colors='bw') +
  geom_line(linetype='dotted', linewidth=1.2, position=position_dodge(width=0.1)) +
  labs(x='Line-Item Veto', y='Probability of Becoming Law', title='', shape='') + 
  theme_bw() +
  geom_text(data=eff_line_law, aes(x=x_text, y=pred_text, label=paste0(round(eff_size_dv, 2), ' SDs'), group=eff_size_dv), 
            inherit.aes = FALSE, hjust=1.2, position=position_dodge(width=0.1), size=6)  +
  theme(text=element_text(size=16),
        axis.text=element_text(size=16),
        legend.position="none")



##########################################################################################################################################################
### Models of interchamber IVs and the proportion of members voting yea

### Models

# Run divided government interaction models
mods_yea_divided_inter_int <- vector(mode='list', length=2)
mods_yea_divided_inter_int[[1]] <- lm(yea_prop ~ veto_override_threshold*divided3 + lineitemveto + maj_sponsor + num_cs + maj_share + pty_diff + mds1 + committee_sponsor + maj_unity + gov_pty_override + as.factor(year), data=filter(votes, !state %in% high_missingness))
mods_yea_divided_inter_int[[2]] <- lm(yea_prop ~ lineitemveto_plot*divided3 + veto_override_threshold + maj_sponsor + num_cs + maj_share + pty_diff + mds1 + committee_sponsor + maj_unity + gov_pty_override + as.factor(year), data=filter(votes, !state %in% high_missingness))


### Substantive effect sizes

# DV standard deviation
yea_sd <- votes %>% filter(!state %in% high_missingness) %>% pull(yea_prop) %>% sd

# Veto override threshold
eff_veto_yea <- plot_model(mods_yea_divided_inter_int[[1]], type='pred', terms=c('veto_override_threshold', 'divided3'))$data %>% 
  as.data.frame() %>%
  filter(x!=0.6) %>%
  dplyr::select(x, predicted, group) %>%
  group_by(group) %>%
  summarise(
    eff_size = diff(predicted),
    pred_text = predicted[x==0.67],
    x_text = max(x)
  ) %>%
  mutate(eff_size_dv=abs(eff_size/yea_sd))

# Line-item veto
eff_line_yea <- plot_model(mods_yea_divided_inter_int[[2]], type='pred', terms=c('lineitemveto_plot', 'divided3'))$data %>% 
  as.data.frame() %>%
  dplyr::select(x, predicted, group) %>%
  group_by(group) %>%
  summarise(
    eff_size = diff(predicted),
    pred_text = predicted[x==2],
    x_text = max(x)
  ) %>%
  mutate(eff_size_dv = abs(eff_size/yea_sd))


### Predicted effects

# Veto override threshold
plot_yea_divided_veto <- plot_model(mods_yea_divided_inter_int[[1]], type='pred', line.size=2,
                                    terms=c('veto_override_threshold', 'divided3'), colors='bw') +
  labs(x='Veto Override Threshold', y='Proportion of Members Voting Yea', title='', linetype='') + 
  theme_bw() +
  geom_text(data=eff_veto_yea, aes(x=x_text, y=pred_text, label=paste0(round(eff_size_dv, 2), ' SDs'), group=eff_size_dv), 
            inherit.aes = FALSE, hjust=1.3, vjust=0.3, size=6) +
  theme(text=element_text(size=16),
        axis.text=element_text(size=16),
        legend.position="none")

# Line item veto
plot_yea_divided_line <- plot_model(mods_yea_divided_inter_int[[2]], type='pred', line.size=2, dot.size=5,
                                    terms=c('lineitemveto_plot', 'divided3'), colors='bw') +
  geom_line(linetype='dotted', linewidth=1.2, position=position_dodge(width=0.1)) +
  labs(x='Line-Item Veto', y='Proportion of Members Voting Yea', title='', shape='') + 
  theme_bw() +
  geom_text(data=eff_line_yea, aes(x=x_text, y=pred_text, label=paste0(round(eff_size_dv, 2), ' SDs'), group=eff_size_dv), 
            inherit.aes = FALSE, hjust=1.2, position=position_dodge(width=-0.1), size=6)  +
  theme(text=element_text(size=16),
        axis.text=element_text(size=16),
        legend.position="none")



##########################################################################################################################################################
### Models of interchamber IVs and the proportion of minority members voting yea

### Models

# Run divided government interaction models
mods_yea_min_divided_inter_int <- vector(mode='list', length=2)
mods_yea_min_divided_inter_int[[1]] <- lm(min_yea_prop ~ veto_override_threshold*divided3 + lineitemveto + maj_sponsor + num_cs + maj_share + pty_diff + mds1 + committee_sponsor + maj_unity + gov_pty_override + as.factor(year), data=filter(votes, !state %in% high_missingness))
mods_yea_min_divided_inter_int[[2]] <- lm(min_yea_prop ~ lineitemveto_plot*divided3 + veto_override_threshold + maj_sponsor + num_cs + maj_share + pty_diff + mds1 + committee_sponsor + maj_unity + gov_pty_override + as.factor(year), data=filter(votes, !state %in% high_missingness))


### Substantive effect sizes

# DV standard deviation
yea_min_sd <- votes %>% filter(!state %in% high_missingness) %>% pull(min_yea_prop) %>% sd(na.rm=TRUE)

# Veto override threshold
eff_veto_yea_min <- plot_model(mods_yea_min_divided_inter_int[[1]], type='pred', terms=c('veto_override_threshold', 'divided3'))$data %>% 
  as.data.frame() %>%
  filter(x!=0.6) %>%
  dplyr::select(x, predicted, group) %>%
  group_by(group) %>%
  summarise(
    eff_size = diff(predicted),
    pred_text = predicted[x==0.67],
    x_text = max(x)
  ) %>%
  mutate(eff_size_dv=abs(eff_size/yea_min_sd))

# Line-item veto
eff_line_yea_min <- plot_model(mods_yea_min_divided_inter_int[[2]], type='pred', terms=c('lineitemveto_plot', 'divided3'))$data %>% 
  as.data.frame() %>%
  dplyr::select(x, predicted, group) %>%
  group_by(group) %>%
  summarise(
    eff_size = diff(predicted),
    pred_text = predicted[x==2],
    x_text = max(x)
  ) %>%
  mutate(eff_size_dv = abs(eff_size/yea_min_sd))


### Predicted effects

# Veto override threshold
plot_yea_min_divided_veto <- plot_model(mods_yea_min_divided_inter_int[[1]], type='pred', line.size=2, 
                                        terms=c('veto_override_threshold', 'divided3'), colors='bw') +
  labs(x='Veto Override Threshold', y='Proportion of Minority Members Voting Yea', title='', linetype='') + 
  theme_bw() +
  geom_text(data=eff_veto_yea_min, aes(x=x_text, y=pred_text, label=paste0(round(eff_size_dv, 2), ' SDs'), group=eff_size_dv), 
            inherit.aes = FALSE, hjust=1.4, vjust=0.5, size=6) +
  theme(text=element_text(size=16),
        axis.text=element_text(size=16),
        legend.position="none")

# Line item veto
plot_yea_min_divided_line <- plot_model(mods_yea_min_divided_inter_int[[2]], type='pred', line.size=2, dot.size=5,
                                        terms=c('lineitemveto_plot', 'divided3'), colors='bw') +
  geom_line(linetype='dotted', linewidth=1.2, position=position_dodge(width=0.1)) +
  labs(x='Line-Item Veto', y='Proportion of Minority Members Voting Yea', title='', shape='') + 
  theme_bw() +
  geom_text(data=eff_line_yea_min, aes(x=x_text, y=pred_text, label=paste0(round(eff_size_dv, 2), ' SDs'), group=eff_size_dv), 
            inherit.aes = FALSE, hjust=1.2, position=position_dodge(width=-0.1), size=6)  +
  theme(text=element_text(size=16),
        axis.text=element_text(size=16),
        legend.position="none")



##########################################################################################################################################################
### Models of interchamber IVs and the probability of a party unity vote

### Models

# Run divided government interaction models
mods_unity_divided_inter_int <- vector(mode='list', length=2)
mods_unity_divided_inter_int[[1]] <- glm(unity_vote ~ veto_override_threshold*divided3 + lineitemveto + maj_sponsor + num_cs + maj_share + pty_diff + mds1 + committee_sponsor + maj_unity + gov_pty_override + as.factor(year), family='binomial', data=filter(votes, !state %in% high_missingness))
mods_unity_divided_inter_int[[2]] <- glm(unity_vote ~ lineitemveto_plot*divided3 + veto_override_threshold + maj_sponsor + num_cs + maj_share + pty_diff + mds1 + committee_sponsor + maj_unity + gov_pty_override + as.factor(year), family='binomial', data=filter(votes, !state %in% high_missingness))


### Substantive effect sizes

# DV observed proportion
unity_sd <- votes %>% 
  filter(!state %in% high_missingness) %>% 
  pull(unity_vote) %>% 
  sd(na.rm=TRUE)

# Veto override threshold
eff_veto_unity <- plot_model(mods_unity_divided_inter_int[[1]], type='pred', terms=c('veto_override_threshold', 'divided3'))$data %>% 
  as.data.frame() %>%
  filter(x!=0.6) %>%
  dplyr::select(x, predicted, group) %>%
  group_by(group) %>%
  summarise(
    eff_size = diff(predicted),
    pred_text = predicted[x==0.5],
    x_text = min(x)
  ) %>%
  mutate(eff_size_dv=abs(eff_size/unity_sd))

# Line-item veto
eff_line_unity <- plot_model(mods_unity_divided_inter_int[[2]], type='pred', terms=c('lineitemveto_plot', 'divided3'))$data %>% 
  as.data.frame() %>%
  dplyr::select(x, predicted, group) %>%
  group_by(group) %>%
  summarise(
    eff_size = diff(predicted),
    pred_text = predicted[x==1],
    x_text = min(x)
  ) %>%
  mutate(eff_size_dv = abs(eff_size/unity_sd))


### Predicted effects

# Veto override threshold
plot_unity_divided_veto <- plot_model(mods_unity_divided_inter_int[[1]], type='pred', line.size=2,
                                      terms=c('veto_override_threshold', 'divided3'), colors='bw') +
  labs(x='Veto Override Threshold', y='Probability of Party Unity Vote', title='', linetype='') + 
  theme_bw() +
  geom_text(data=eff_veto_unity, aes(x=x_text, y=pred_text, label=paste0(round(eff_size_dv, 2), ' SDs'), 
                                     group=eff_size_dv), inherit.aes = FALSE, hjust=-0.8, vjust=1, size=6) +
  theme(text=element_text(size=16),
        axis.text=element_text(size=16),
        legend.position="none")

# Line item veto
plot_unity_divided_line <- plot_model(mods_unity_divided_inter_int[[2]], type='pred', line.size=2, dot.size=5,
                                      terms=c('lineitemveto_plot', 'divided3'), colors='bw') +
  geom_line(linetype='dotted', linewidth=1.2, position=position_dodge(width=0.1)) +
  labs(x='Line-Item Veto', y='Probability of Party Unity Vote', title='', shape='') + 
  theme_bw() +
  geom_text(data=eff_line_unity, aes(x=x_text, y=pred_text, label=paste0(round(eff_size_dv, 2), ' SDs'), group=eff_size_dv), 
            inherit.aes = FALSE, hjust=-0.2, position=position_dodge(width=-0.1), size=6)  +
  theme(text=element_text(size=16),
        axis.text=element_text(size=16),
        legend.position="none")



##########################################################################################################################################################
### Combine plots by IV

# Figure 2: Line item veto
line_leg <- plot_model(mods_unity_divided_inter_int[[2]], type='pred', dot.size=5,
                       terms=c('lineitemveto_plot', 'divided3'), colors='bw') +
  labs(shape='Legend') + theme_bw() +
  theme(legend.text=element_text(size=20), legend.title=element_text(size=20)) +
  guides(shape = guide_legend(override.aes = list(size = 15)))
line_leg_plot <- cowplot::get_legend(line_leg) %>%
  as_ggplot() + theme(legend.key.size=unit(10,'cm'))
plot_line <- plot_vote_divided_line + plot_law_divided_line + plot_yea_divided_line + plot_yea_min_divided_line + plot_unity_divided_line + line_leg_plot + plot_layout(ncol=2)
ggsave("results/fig2_line_divided_plot.pdf", width=12, height=15, plot=plot_line)

# Figure 3: Veto override
veto_leg <- plot_model(mods_unity_divided_inter_int[[1]], type='pred', line.size=2,
                       terms=c('veto_override_threshold', 'divided3'), colors='bw') +
  labs(linetype='Legend') + theme_bw() +
  theme(legend.text=element_text(size=20), legend.title=element_text(size=20)) +
  guides(linetype = guide_legend(override.aes = list(size = 20)))
veto_leg_plot <- cowplot::get_legend(veto_leg) %>%
  as_ggplot() + theme(legend.key.size=unit(10,'cm'))
plot_veto <- plot_vote_divided_veto + plot_law_divided_veto + plot_yea_divided_veto + plot_yea_min_divided_veto + plot_unity_divided_veto + veto_leg_plot + plot_layout(ncol=2)
ggsave("results/fig3_veto_divided_plot.pdf", width=12, height=15, plot=plot_veto)
